require('../js/global.js');
var ServerManager = require('../js/servers.js').ServerManager;
global.gServers = new ServerManager();
var logic = require('./logic');

global.gPrintHelp = function() {
    console.log(util.format('node gm.js command server[all|sid|minsid-maxsid] [args ...]'));
    console.log(util.format('可选公共args:'));
    console.log(util.format('   openids=file 指定一个文件名,该文件里是openid列表,一行一个'));
    console.log(util.format('   openid=xxx 指定一个openid'));
    console.log(util.format('   uids=file 指定一个文件名,该文件里是uid列表,一行一个'));
    console.log(util.format('   uid=num 指定一个uid'));
    for (var name in logic) {
        var help = logic[name].help;
        if (help) {
            var messages = help();
            console.log(util.format("%s args :%s", name, messages[0]));
            for (var i = 1; i < messages.length; ++i) {
                console.log(util.format("   %s", messages[i]));
            }
        }
    }
}

main();

function main() {
    var argv = process.argv;

    if (argv.length <= 3) {
        ERROR(util.format('need more arg'));
        gPrintHelp();
        process.exit(1);
    }

    var cmd = argv[2];
    var serverDesc = argv[3];

    var mod = logic[cmd];
    if (!mod) {
        ERROR(util.format('invalid command'));
        gPrintHelp();
        process.exit(1);
    }

    if (!mod.handler) {
        ERROR(util.format('invalid handler'));
        gPrintHelp();
        process.exit(1);
    }
    
    var query = {};
    for (var i = 4, len = argv.length; i < len; ++i) {
        var segs = argv[i].split("=");
        if (2 == segs.length) {
            var key = segs[0];
            var value = segs[1];
            if (!isNaN(value)) {
                value = +value;
            }

            if ('openids' == key) {
                var data = fs.readFileSync(value, 'UTF-8');
                value = data.split('\n');
            } else if ('uids' == key) {
                var data = fs.readFileSync(value, 'UTF-8');
                value = data.split('\n');
                for (var i = 0; i < value.length; ++i) {
                    value[i] = +value[i];
                }
            }

            query[key] = value;
        }
    }

    if (mod.checkQuery && !mod.checkQuery(query)) {
        ERROR(util.format('invalid args'));
        process.exit(1);
    }

    if (mod.getTitle) {
        mod.getTitle(query);
    }

    var useSlaveDB = mod.master ? false : true;
    async.series([
        function (cb) {
            gServers.load({slave:useSlaveDB, native:true}, function(err) {
                cb(err);
            });
        },
        function (cb) {
            var sidArr = gServers.getDescSids(serverDesc);
            if (0 == sidArr.length) {
                ERROR(util.format('server=%s invalid', serverDesc));
                cb(); return;
            }
            
            if (mod.batch) {
                mod.handler(sidArr, query, cb);
            } else {
                async.each(gServers.getDescSids(serverDesc), 
                function(sid, scb) {
                    mod.handler(sid, query, scb);
                }, 
                function(err) {
                    cb(err);
                });
            }
        },
    ], function(err, results) {
        if (err) {
            ERROR(util.format('end with err: %j', err));
        }

        process.exit(0);
    });
}
